package com.leinao.project.controller;

import com.alibaba.fastjson.JSON;
import com.leinao.commons.Response;
import com.leinao.constant.Constant;
import com.leinao.constant.LoginEnum;
import com.leinao.job.dto.PaiJobEnvironment;
import com.leinao.job.dto.ProjectJobViewDto;
import com.leinao.login.dto.LoginResultDto;
import com.leinao.project.service.PublicProjectService;
import com.leinao.util.page.PageInfo;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;

/**
 * @Auther: weiwei
 * @Date: 2019/6/6 15:17
 * @Description:
 * @Version 1.0
 */
@RestController
@CrossOrigin
public class PublicProjectController {

    private static Logger logger = LoggerFactory.getLogger(PublicProjectController.class);

    @Autowired
    private PublicProjectService publicProjectService;


    @ApiOperation(value = "获取公开用户任务详情", notes = "获取公开用户任务详情-右侧")
    @GetMapping(value = "/project/getUserPublicTaskDetail")
    public Response<ProjectJobViewDto> getUserPublicTaskDetail(HttpServletRequest request,
                                                               @ApiParam(value = "公开任务ID", required = true) @RequestParam Long publicJobId){

        logger.info(String.format("getUserPublicTaskDetail publicJobId：", publicJobId));

        String obj = (String) request.getSession().getAttribute(Constant.USER_SESSION_KEY);
        LoginResultDto userInfo = JSON.parseObject(obj, LoginResultDto.class);
        if (null == userInfo && StringUtils.isBlank(userInfo.getUserId())) {
            return Response.error(Constant.RESPONSE_STATUS_SUCCESS, LoginEnum.LOGIN_TIMEOUT.getSeq(), LoginEnum.LOGIN_TIMEOUT.getDesc(), null);
        }

        return publicProjectService.getUserPublicTaskDetail(publicJobId, userInfo.getToken());
    }

    @ApiOperation(value = "获取公开项目的任务列表", notes = "公开项目详情页使用")
	@GetMapping(value = "/project/getProjectPublicJob")
	public Response<PageInfo> getProjectPublicJob(@ApiParam(value = "当前页") @RequestParam(required = false) Long projectId,
                                                  @ApiParam(value = "当前页") @RequestParam(required = false) Integer pageNumber,
                                                  @ApiParam(value = "每页条数") @RequestParam(required = false) Integer pageSize) {
        logger.info("getProjectPublicJob param projectId: {}  pageNumber: {}, pageSize: {}", projectId, pageNumber, pageSize);

        // 默认分页条件设置
        if (pageNumber == null || pageNumber < Constant.DEFAULT_PAGE_NUMBER) {
            pageNumber = Constant.DEFAULT_PAGE_NUMBER;
        }
        if (pageSize == null || pageSize < Constant.DEFAULT_PAGE_NUMBER) {
            pageSize = Constant.DEFAULT_PAGE_SIZE;
        }

        return publicProjectService.getProjectPublicJob(projectId, pageNumber, pageSize);
    }

    @ApiOperation(value = "查看公开任务信息", notes = "查看公开任务信息")
    @PostMapping(value = "/getJobInfo")
    public Response<Object> getJobInfo(HttpServletRequest request,
                                       @ApiParam(value = "公开任务ID", required = true) @RequestParam Long publicJobId) {
        logger.info("getJobInfo publicJobId： {}", publicJobId );

        String obj = (String) request.getSession().getAttribute(Constant.USER_SESSION_KEY);
        LoginResultDto userInfo = JSON.parseObject(obj, LoginResultDto.class);

        return publicProjectService.getJobInfo(publicJobId, userInfo.getToken());
    }

    @ApiOperation(value = "获取任务语言框架", notes = "获取任务语言框架")
    @PostMapping(value = "/getJobEnvironment")
    public Response<PaiJobEnvironment> getJobEnvironment(HttpServletRequest request,
                                                         @ApiParam(value = "公开任务ID", required = true) @RequestParam Long publicJobId) {
        logger.info("getJobEnvironment jobName： {}", publicJobId );

        String obj = (String) request.getSession().getAttribute(Constant.USER_SESSION_KEY);
        LoginResultDto userInfo = JSON.parseObject(obj, LoginResultDto.class);

        return publicProjectService.getJobEnvironment(publicJobId, userInfo.getToken());
    }

    @ApiOperation(value = "获取任务参数", notes = "获取任务参数")
    @PostMapping(value = "/getJobParams")
    public Response<String> getJobParams(HttpServletRequest request,
                                         @ApiParam(value = "公开任务ID", required = true) @RequestParam Long publicJobId) {
        logger.info("getJobParams publicJobId： {}", publicJobId );

        String obj = (String) request.getSession().getAttribute(Constant.USER_SESSION_KEY);
        LoginResultDto userInfo = JSON.parseObject(obj, LoginResultDto.class);

        return publicProjectService.getJobParams(publicJobId, userInfo.getToken());
    }


}
